package inftel.android;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class PosicionUsuario extends MapActivity{
	
	private int idUsuario = 1;
	public double flat;
	public double flon;
	
	//lista donde estan los markers (los puntos que queramos mostrar)
	List<Overlay> mapOverlays;
	
	private MapView mapView;
	
	//para las imagenes a mostrar
	Drawable drawable;
	
	//clase ya creada
	ItemizedOverlay itemizedOverlayMiPosicion;
	
	MapController mc;
	
	private LocationManager locationManager;

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    requestWindowFeature(Window.FEATURE_NO_TITLE);
	    setContentView(R.layout.mapas);
	    
	    mapView = (MapView) findViewById(R.id.mapview);
	    mapView.setBuiltInZoomControls(true);
	    mapView.setSatellite(true);
		mc = mapView.getController();
		
		mc.setZoom(4);
	    
	   // mapOverlays = mapView.getOverlays();
	    //drawable = this.getResources().getDrawable(R.drawable.androidmarker);
	    //itemizedoverlay = new ItemizedOverlay(drawable);
	    
	    //GeoPoint point = new GeoPoint(19240000,-99120000);
	    //OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
	    
	    //GeoPoint point2 = new GeoPoint(35410000, 139460000);
	    //OverlayItem overlayitem2 = new OverlayItem(point2, "Sekai, konichiwa!", "I'm in Japan!");
	    
	    //itemizedoverlay.addOverlay(overlayitem);
	    //itemizedoverlay.addOverlay(overlayitem2);
	    //mapOverlays.add(itemizedoverlay);
	    
	    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, new GeoUpdateHandler());
	}
	
	public void pintaMiPosicion (int latitud, int longitud){
		
		
		mapOverlays = mapView.getOverlays();
	    drawable = this.getResources().getDrawable(R.drawable.androidmarker);
	    itemizedOverlayMiPosicion = new ItemizedOverlay(drawable);
		
		GeoPoint point = new GeoPoint(latitud,longitud);
	    OverlayItem overlayitem = new OverlayItem(point, " ", " ");
	    
	    
	    //limpio los puntos ya metidos
	    mapOverlays.clear();
	    
	    itemizedOverlayMiPosicion.addOverlay(overlayitem);
	    mapOverlays.add(itemizedOverlayMiPosicion);
	    
	    mc.setZoom(12);
	    mc.setCenter(point);
		
	}
	
	
	public class GeoUpdateHandler implements LocationListener {

		@Override
		public void onLocationChanged(Location location) {

			int lat = (int) (location.getLatitude() * 1E6);
			int lng = (int) (location.getLongitude() * 1E6);

			String servletURL = "http://10.0.2.2:8080/caralibro-war/EnviarMiLocalizacionServlet";

			try {

				URL ob = new URL(servletURL + "?idUsuario=" + idUsuario
						+ "&latitud=" + lat + "&longitud=" + lng);

				URLConnection conn = ob.openConnection();

				conn.setDoInput(true);
				conn.setDoOutput(true);

				Log.i("-------URL", ob.toString());

				InputStream in = conn.getInputStream();
				DataInputStream dos = new DataInputStream(in);
				String leido = dos.readLine();

			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			flat = location.getLatitude();
			flon = location.getLongitude();

			new Thread() {
				@Override
				public void run() {
					
					pintaMiPosicion((int) (flat * 1E6), (int) (flon * 1E6));

				}
			}.start();

			// mapController.setZoom(18);
			// mapController.setCenter(point);
		}

		@Override
		public void onProviderDisabled(String provider) {
		}

		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	}

}
